/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.forms.nhanh;

import com.aptech.beans.Drugs;
import com.aptech.dao.DrugManager;
import com.aptech.validate.AccountValidator;
import com.aptech.validate.CustomerValidator;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Anh Nguyen
 */
public class DrugList extends javax.swing.JFrame {

    private String flag = "";
    DefaultTableModel tableModel;

    /**
     * Creates new form DrugList
     */
    public DrugList() {
        initComponents();
        setLocationRelativeTo(null);
        loadData();
    }

    class ComboItem {

        private String key;
        private String value;

        public ComboItem(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String toString() {
            return key;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jdDrugList_InsertUpdate = new javax.swing.JDialog();
        jPanel1 = new javax.swing.JPanel();
        jtfDrugList_Name = new javax.swing.JTextField();
        jlblDrugList_Name = new javax.swing.JLabel();
        jtfDrugList_Unit = new javax.swing.JTextField();
        jlblDrugList_Unit = new javax.swing.JLabel();
        jlblDrugList_UnitPrice = new javax.swing.JLabel();
        jlblDrugList_StockAvailable = new javax.swing.JLabel();
        jlblDrugList_Description = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtaDrugList_Description = new javax.swing.JTextArea();
        jtfDrugList_UnitPrice = new javax.swing.JTextField();
        jtfDrugList_StockAvailable = new javax.swing.JTextField();
        jlblDrugList_SupplierID = new javax.swing.JLabel();
        cbSupplierID = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jbtDrugList_Submit = new javax.swing.JButton();
        jbtDrugList_Reset = new javax.swing.JButton();
        jdDrugList_Notification = new javax.swing.JDialog();
        jLabel10 = new javax.swing.JLabel();
        jlblUnit = new javax.swing.JLabel();
        jlblName = new javax.swing.JLabel();
        jlblStatus_Name = new javax.swing.JLabel();
        jlblStatus_Unit = new javax.swing.JLabel();
        jlblStatus_UnitPrice = new javax.swing.JLabel();
        jlblStatus_StockAvailable = new javax.swing.JLabel();
        jlbStockAvailable = new javax.swing.JLabel();
        jlblUnitPrice = new javax.swing.JLabel();
        jlblDescription = new javax.swing.JLabel();
        jlblStatus_Description = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnInsert = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDrugList = new javax.swing.JTable();
        jlblAccList_Username1 = new javax.swing.JLabel();
        jtfDrugList_Search = new javax.swing.JTextField();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(255, 0, 0))); // NOI18N

        jtfDrugList_Name.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblDrugList_Name.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_Name.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_Name.setText("Name");

        jtfDrugList_Unit.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblDrugList_Unit.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_Unit.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_Unit.setText("Unit");

        jlblDrugList_UnitPrice.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_UnitPrice.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_UnitPrice.setText("Unit Price");

        jlblDrugList_StockAvailable.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_StockAvailable.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_StockAvailable.setText("Stock Available");

        jlblDrugList_Description.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_Description.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_Description.setText("Description");

        jtaDrugList_Description.setColumns(20);
        jtaDrugList_Description.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jtaDrugList_Description.setRows(5);
        jScrollPane4.setViewportView(jtaDrugList_Description);

        jtfDrugList_UnitPrice.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jtfDrugList_StockAvailable.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblDrugList_SupplierID.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblDrugList_SupplierID.setForeground(new java.awt.Color(51, 0, 255));
        jlblDrugList_SupplierID.setText("Supplier ID");

        cbSupplierID.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblDrugList_SupplierID)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jlblDrugList_Name, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jlblDrugList_Unit, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jlblDrugList_UnitPrice, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jlblDrugList_Description, javax.swing.GroupLayout.Alignment.TRAILING)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jlblDrugList_StockAvailable))))
                .addGap(61, 61, 61)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jtfDrugList_Name)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addComponent(jtfDrugList_Unit)
                        .addComponent(jtfDrugList_UnitPrice)
                        .addComponent(jtfDrugList_StockAvailable))
                    .addComponent(cbSupplierID, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblDrugList_Name)
                    .addComponent(jtfDrugList_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDrugList_SupplierID)
                    .addComponent(cbSupplierID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDrugList_Unit)
                    .addComponent(jtfDrugList_Unit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDrugList_UnitPrice)
                    .addComponent(jtfDrugList_UnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDrugList_StockAvailable)
                    .addComponent(jtfDrugList_StockAvailable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblDrugList_Description)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/029.png"))); // NOI18N

        jbtDrugList_Submit.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtDrugList_Submit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/003.png"))); // NOI18N
        jbtDrugList_Submit.setText("Submit");
        jbtDrugList_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtDrugList_SubmitActionPerformed(evt);
            }
        });

        jbtDrugList_Reset.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtDrugList_Reset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/016.png"))); // NOI18N
        jbtDrugList_Reset.setText("Reset");
        jbtDrugList_Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtDrugList_ResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jdDrugList_InsertUpdateLayout = new javax.swing.GroupLayout(jdDrugList_InsertUpdate.getContentPane());
        jdDrugList_InsertUpdate.getContentPane().setLayout(jdDrugList_InsertUpdateLayout);
        jdDrugList_InsertUpdateLayout.setHorizontalGroup(
            jdDrugList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdDrugList_InsertUpdateLayout.createSequentialGroup()
                .addGroup(jdDrugList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jdDrugList_InsertUpdateLayout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(jbtDrugList_Submit)
                        .addGap(49, 49, 49)
                        .addComponent(jbtDrugList_Reset))
                    .addGroup(jdDrugList_InsertUpdateLayout.createSequentialGroup()
                        .addGap(198, 198, 198)
                        .addComponent(jLabel9))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jdDrugList_InsertUpdateLayout.setVerticalGroup(
            jdDrugList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jdDrugList_InsertUpdateLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel9)
                .addGap(26, 26, 26)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(jdDrugList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtDrugList_Submit)
                    .addComponent(jbtDrugList_Reset))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(153, 0, 153));
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/031.png"))); // NOI18N
        jLabel10.setText("Notification");
        jLabel10.setToolTipText("");

        jlblUnit.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUnit.setForeground(new java.awt.Color(255, 102, 102));
        jlblUnit.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUnit.setText("Unit:");

        jlblName.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblName.setForeground(new java.awt.Color(255, 102, 102));
        jlblName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblName.setText("Name:");

        jlblStatus_Name.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Name.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Name.setText("field");

        jlblStatus_Unit.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Unit.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Unit.setText("field");

        jlblStatus_UnitPrice.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_UnitPrice.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_UnitPrice.setText("field");

        jlblStatus_StockAvailable.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_StockAvailable.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_StockAvailable.setText("field");

        jlbStockAvailable.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlbStockAvailable.setForeground(new java.awt.Color(255, 102, 102));
        jlbStockAvailable.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlbStockAvailable.setText("Stock Available:");

        jlblUnitPrice.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUnitPrice.setForeground(new java.awt.Color(255, 102, 102));
        jlblUnitPrice.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUnitPrice.setText("Unit Price:");

        jlblDescription.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblDescription.setForeground(new java.awt.Color(255, 102, 102));
        jlblDescription.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblDescription.setText("Description:");

        jlblStatus_Description.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Description.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Description.setText("field");

        javax.swing.GroupLayout jdDrugList_NotificationLayout = new javax.swing.GroupLayout(jdDrugList_Notification.getContentPane());
        jdDrugList_Notification.getContentPane().setLayout(jdDrugList_NotificationLayout);
        jdDrugList_NotificationLayout.setHorizontalGroup(
            jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdDrugList_NotificationLayout.createSequentialGroup()
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jdDrugList_NotificationLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblUnitPrice, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlblUnit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblDescription, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jdDrugList_NotificationLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jlbStockAvailable)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblStatus_StockAvailable, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblStatus_Description, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblStatus_Name, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblStatus_Unit, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblStatus_UnitPrice, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
            .addGroup(jdDrugList_NotificationLayout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(jLabel10)
                .addContainerGap(109, Short.MAX_VALUE))
        );
        jdDrugList_NotificationLayout.setVerticalGroup(
            jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdDrugList_NotificationLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel10)
                .addGap(62, 62, 62)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblName, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Name))
                .addGap(18, 18, 18)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Unit))
                .addGap(18, 18, 18)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_UnitPrice))
                .addGap(29, 29, 29)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbStockAvailable, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_StockAvailable))
                .addGap(26, 26, 26)
                .addGroup(jdDrugList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Description))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Drugs Management");
        setIconImage(new ImageIcon(getClass().getResource("/com/image/icon.png")).getImage());

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/036.png"))); // NOI18N
        jLabel1.setText("Drugs Management");
        jLabel1.setMaximumSize(new java.awt.Dimension(263, 64));
        jLabel1.setMinimumSize(new java.awt.Dimension(263, 64));
        jLabel1.setPreferredSize(new java.awt.Dimension(263, 64));

        btnInsert.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/026.png"))); // NOI18N
        btnInsert.setText("Insert");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/027.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/028.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jtbDrugList.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jtbDrugList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Supplier Name", "Name", "Unit", "UnitPrice", "Stock", "Description"
            }
        ));
        jtbDrugList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jtbDrugList);

        jlblAccList_Username1.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username1.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/057.png"))); // NOI18N

        jtfDrugList_Search.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jtfDrugList_Search.setToolTipText("Search by Name");
        jtfDrugList_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDrugList_SearchKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jlblAccList_Username1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDrugList_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 715, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(115, 115, 115)
                .addComponent(btnInsert)
                .addGap(72, 72, 72)
                .addComponent(btnUpdate)
                .addGap(71, 71, 71)
                .addComponent(btnDelete)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(220, 220, 220))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jtfDrugList_Search, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblAccList_Username1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsert)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        // TODO add your handling code here:
        jdDrugList_InsertUpdate.setVisible(true);
        jdDrugList_InsertUpdate.pack();
        jdDrugList_InsertUpdate.setLocationRelativeTo(null);
        jtfDrugList_Name.setEditable(true);
        flag = "insert";
        clearForm();
    }//GEN-LAST:event_btnInsertActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        flag = "update";
        clearForm();
        if (jtbDrugList.getSelectedRowCount() == 1) {
            jdDrugList_InsertUpdate.setVisible(true);
            jdDrugList_InsertUpdate.pack();
            jdDrugList_InsertUpdate.setLocationRelativeTo(null);

            try {
                ResultSet resultSet = DrugManager.displayAllRows();
                resultSet.absolute(jtbDrugList.getSelectedRow() + 1);
                // Dang cho xu ly
                ComboItem item;
                for (int i = 0; i < cbSupplierID.getItemCount(); i++) {
                    item = (ComboItem) cbSupplierID.getItemAt(i);
//                    System.out.println(item.getKey());
//                    System.out.println(tableModel.getValueAt(jtbDrugList.getSelectedRow(), 1));
                    if (item.getKey().equals(tableModel.getValueAt(jtbDrugList.getSelectedRow(), 1))) {
                        cbSupplierID.setSelectedIndex(i);
                        break;
                    }
                }
//                cbSupplierID.setSelectedIndex(DrugManager.checkSuppIDByName(resultSet.getString(2)).getSupplierID());
                jtfDrugList_Name.setText(resultSet.getString(3).trim());
                jtfDrugList_Unit.setText(resultSet.getString(4).trim());
                jtfDrugList_UnitPrice.setText(String.valueOf(resultSet.getDouble(5)).trim());
                jtfDrugList_StockAvailable.setText(String.valueOf(resultSet.getInt(6)).trim());
                jtaDrugList_Description.setText(resultSet.getString(7).trim());
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please choose a record to update!", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

//    public static void setSelectedValue(JComboBox comboBox, int value)
//    {
//        ComboItem item;
//        for (int i = 0; i < comboBox.getItemCount(); i++)
//        {
//            item = (ComboItem)comboBox.getItemAt(i);
//            if (item.getValue() == value)
//            {
//                comboBox.setSelectedIndex(i);
//                break;
//            }
//        }
//    }
    /* Delete data */
    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (jtbDrugList.getSelectedRowCount() == 1) {
            int reply = JOptionPane.showConfirmDialog(null, "Do you want to delete this record?", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
            if (reply == JOptionPane.YES_OPTION) {
                try {
                    ResultSet resultSet = DrugManager.displayAllRows();
                    resultSet.absolute(jtbDrugList.getSelectedRow() + 1);
                    resultSet.deleteRow();
                    loadData();
                } catch (SQLException ex) {
                    System.out.println(ex.getMessage());
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(null, "Please choose a record to delete!", "Message", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void jbtDrugList_ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDrugList_ResetActionPerformed
        // TODO add your handling code here:
        clearForm();
    }//GEN-LAST:event_jbtDrugList_ResetActionPerformed

    private void jbtDrugList_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDrugList_SubmitActionPerformed
        // TODO add your handling code here:
        boolean checkName = AccountValidator.validataDrugName(jtfDrugList_Name.getText().trim());
        boolean checkUnit = AccountValidator.validataDrugName(jtfDrugList_Unit.getText().trim());
        boolean checkUnitPrice = CustomerValidator.checkNumber(jtfDrugList_UnitPrice.getText().trim());
        boolean checkStock = CustomerValidator.checkNumber(jtfDrugList_StockAvailable.getText().trim());

        try {
            //Get data
            Drugs drug = new Drugs();
//            drug.setSupplierID(Integer.parseInt(jlblDrugList_SupplierID.getText().trim()));

            //Insert data
            if (flag.equals("insert")) {
                if (checkName && checkUnit && checkUnitPrice && checkStock) {
                    if (DrugManager.checkName(jtfDrugList_Name.getText().trim()) == null) {
                        drug.setDrugname(jtfDrugList_Name.getText().trim());
                        drug.setUnit(jtfDrugList_Unit.getText().trim());
                        drug.setUnitprice(Double.parseDouble(jtfDrugList_UnitPrice.getText().trim()));
                        drug.setStockavailable(Integer.parseInt(jtfDrugList_StockAvailable.getText().trim()));
                        drug.setDescription(jtaDrugList_Description.getText().trim());
                        //Get selected item combobox SupplierID
                        ComboItem item = (ComboItem) cbSupplierID.getSelectedItem();
                        drug.setSupplierID(Integer.parseInt(item.getValue()));
                        boolean result = DrugManager.insert(drug);
                        if (result) {
                            JOptionPane.showMessageDialog(this, "Insert successfully!", "Message", JOptionPane.INFORMATION_MESSAGE);
                            loadData();
                            jdDrugList_InsertUpdate.dispose();
                        } else {
                            JOptionPane.showMessageDialog(this, "Insert failed!", "Message", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "Existing Drug Name!", "Message", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    jdDrugList_Notification.setVisible(true);
                    jdDrugList_Notification.pack();

                    ImageIcon correctIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/032.png"));
                    ImageIcon falseIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/033.png"));

                    jlblStatus_Name.setIcon(checkName ? correctIcon : falseIcon);
                    jlblStatus_Unit.setIcon(checkUnit ? correctIcon : falseIcon);
                    jlblStatus_UnitPrice.setIcon(checkUnitPrice ? correctIcon : falseIcon);
                    jlblStatus_StockAvailable.setIcon(checkStock ? correctIcon : falseIcon);
                }
            } else if (flag.equals("update")) {
                if (checkName && checkUnit && checkUnitPrice && checkStock) {
//                    drug.setSupplierID(Integer.parseInt(String.valueOf(tableModel.getValueAt(jtbDrugList.getSelectedRow(), 1))));
                    drug.setDrugname(jtfDrugList_Name.getText().trim());
                    drug.setUnit(jtfDrugList_Unit.getText().trim());
                    drug.setUnitprice(Double.parseDouble(jtfDrugList_UnitPrice.getText().trim()));
                    drug.setStockavailable(Integer.parseInt(jtfDrugList_StockAvailable.getText().trim()));
                    drug.setDescription(jtaDrugList_Description.getText().trim());
                    //Get selected item combobox SupplierID
                    ComboItem item = (ComboItem) cbSupplierID.getSelectedItem();
                    drug.setSupplierID(Integer.parseInt(item.getValue()));
                    drug.setDrugID(Integer.parseInt(String.valueOf(tableModel.getValueAt(jtbDrugList.getSelectedRow(), 0))));
                    ComboItem selectedItem = (ComboItem) cbSupplierID.getSelectedItem();
//                    System.out.println(selectedItem.getKey());
                    drug.setSupplierID(Integer.parseInt(selectedItem.getValue()));
                    System.out.println(drug.getDrugID() + "-" + drug.getDrugname() + "-" + drug.getSupplierID());
                    boolean result = DrugManager.update(drug);

                    if (result) {
                        JOptionPane.showMessageDialog(this, "Update Successfully!", "Message", JOptionPane.INFORMATION_MESSAGE);
                        loadData();
                        jdDrugList_InsertUpdate.dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "Update Failed!", "Message", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    jdDrugList_Notification.setVisible(true);
                    jdDrugList_Notification.pack();

                    ImageIcon correctIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/032.png"));
                    ImageIcon falseIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/033.png"));

                    jlblStatus_Name.setIcon(checkName ? correctIcon : falseIcon);
                    jlblStatus_Unit.setIcon(checkUnit ? correctIcon : falseIcon);
                    jlblStatus_UnitPrice.setIcon(checkUnitPrice ? correctIcon : falseIcon);
                    jlblStatus_StockAvailable.setIcon(checkStock ? correctIcon : falseIcon);
                }
            }
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(DrugList.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jbtDrugList_SubmitActionPerformed

    private void jtfDrugList_SearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDrugList_SearchKeyPressed

        jtfDrugList_Search.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent de) {
                newFilter();
            }

            @Override
            public void removeUpdate(DocumentEvent de) {
                newFilter();
            }

            @Override
            public void changedUpdate(DocumentEvent de) {
                newFilter();
            }

            private void newFilter() {
                TableRowSorter sorter = new TableRowSorter<>(tableModel);
                jtbDrugList.setRowSorter(sorter);
                // Fillter data by cloumn = 1
                RowFilter<DefaultTableModel, Object> rowFilter = RowFilter.regexFilter(jtfDrugList_Search.getText(), 2);
                sorter.setRowFilter(rowFilter);
            }
        });
    }//GEN-LAST:event_jtfDrugList_SearchKeyPressed

    /* Load data to table */
    private void loadData() {
        tableModel = (DefaultTableModel) jtbDrugList.getModel();
        tableModel.setRowCount(0);
        try {
            ResultSet resultSet = DrugManager.displayAllRows();

            while (resultSet.next()) {
                Vector vector = new Vector();
                vector.add(String.valueOf(resultSet.getInt(1)).trim());
                vector.add(String.valueOf(resultSet.getString(2)).trim());
                vector.add(resultSet.getString(3).trim());
                vector.add(resultSet.getString(4).trim());
                vector.add(String.valueOf(resultSet.getDouble(5)).trim());
                vector.add(String.valueOf(resultSet.getInt(6)).trim());
                vector.add(resultSet.getString(7).trim());
                tableModel.addRow(vector);
            }
            // Fill data into combobox cbSupplierID
            if (cbSupplierID.getItemCount() == 0) {
                ResultSet rsSuppID = DrugManager.getSuppID();
                while (rsSuppID.next()) {
                    cbSupplierID.addItem(new DrugList.ComboItem(rsSuppID.getString(2), String.valueOf(rsSuppID.getInt(1)).trim()));
//                        System.out.println(rsSuppID.getInt(1) + "-" + rsSuppID.getString(2) + "\n");
                }
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /* Clearn form */
    private void clearForm() {
        jtfDrugList_Name.setText(null);
//        cbSupplierID.setSelectedIndex(0);
        jtfDrugList_Unit.setText(null);
        jtfDrugList_UnitPrice.setText(null);
        jtfDrugList_StockAvailable.setText(null);
        jtaDrugList_Description.setText(null);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DrugList.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DrugList.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DrugList.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DrugList.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new DrugList().setVisible(true);

                } catch (Exception ex) {
                    Logger.getLogger(DrugList.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbSupplierID;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton jbtDrugList_Reset;
    private javax.swing.JButton jbtDrugList_Submit;
    private javax.swing.JDialog jdDrugList_InsertUpdate;
    private javax.swing.JDialog jdDrugList_Notification;
    private javax.swing.JLabel jlbStockAvailable;
    private javax.swing.JLabel jlblAccList_Username1;
    private javax.swing.JLabel jlblDescription;
    private javax.swing.JLabel jlblDrugList_Description;
    private javax.swing.JLabel jlblDrugList_Name;
    private javax.swing.JLabel jlblDrugList_StockAvailable;
    private javax.swing.JLabel jlblDrugList_SupplierID;
    private javax.swing.JLabel jlblDrugList_Unit;
    private javax.swing.JLabel jlblDrugList_UnitPrice;
    private javax.swing.JLabel jlblName;
    private javax.swing.JLabel jlblStatus_Description;
    private javax.swing.JLabel jlblStatus_Name;
    private javax.swing.JLabel jlblStatus_StockAvailable;
    private javax.swing.JLabel jlblStatus_Unit;
    private javax.swing.JLabel jlblStatus_UnitPrice;
    private javax.swing.JLabel jlblUnit;
    private javax.swing.JLabel jlblUnitPrice;
    private javax.swing.JTextArea jtaDrugList_Description;
    private javax.swing.JTable jtbDrugList;
    private javax.swing.JTextField jtfDrugList_Name;
    private javax.swing.JTextField jtfDrugList_Search;
    private javax.swing.JTextField jtfDrugList_StockAvailable;
    private javax.swing.JTextField jtfDrugList_Unit;
    private javax.swing.JTextField jtfDrugList_UnitPrice;
    // End of variables declaration//GEN-END:variables

}
